package com.nand.module.function.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.request.RequestOptions;
import com.nand.module.function.R;
import com.nand.module.function.bean.IqcListBean;
import com.nand.module.function.bean.MMMDetailBean;

import java.util.ArrayList;
import java.util.List;

/**
 * created by 86185
 * on 2020/1/17
 */
public class MMMDetailsAdapter extends RecyclerView.Adapter<MMMDetailsAdapter.ViewHolder> implements View.OnClickListener {
    private Context mContext;
    private int type;
    private List<MMMDetailBean> mList = new ArrayList<>();
    private String cont = "";
    private int code = 0;

    private MMMDetailsAdapter.OnItemClickListener mOnItemClickListener = null;
    private String replace;
    private RequestOptions error;

    public MMMDetailsAdapter(Context mContext, int type) {
        this.mContext = mContext;
        this.type = type;
    }

    public void setData(List<MMMDetailBean> data) {
        this.mList = data;
        notifyDataSetChanged();
    }

    public List<MMMDetailBean> getCheckItemsBean() {
        return mList;
    }


    @Override
    public MMMDetailsAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_mmm_details, viewGroup, false);
        layout.setOnClickListener(this);
        return new MMMDetailsAdapter.ViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(final MMMDetailsAdapter.ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        final MMMDetailBean bean = mList.get(position);
        int code = position + 1;
        viewHolder.mTextview1.setText(code+"");
        viewHolder.mTextview2.setText(bean.getPartId());
        viewHolder.mTextview3.setText(bean.getPartname());
        viewHolder.mTextview4.setText(bean.getNeedNumber()+"");
        switch (type){
            case 1:
                viewHolder.mTextview5.setVisibility(View.GONE);
                viewHolder.mEtCks.setVisibility(View.VISIBLE);

                viewHolder.mEtCks.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before,
                                              int count) {
                        cont = s.toString();
                        mList.get(position).setOutNumber(cont);
                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
                break;
            case 2:
                viewHolder.mTextview5.setVisibility(View.VISIBLE);
                viewHolder.mEtCks.setVisibility(View.GONE);
                viewHolder.mTextview5.setText(bean.getOutNumber()+"");
                break;
        }

        viewHolder.mTextview2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener!=null){
                    mOnItemClickListener.onWlbmClick(position,bean.getPartId(),viewHolder.mTextview2);
                }
            }
        });

        viewHolder.mTextview3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener!=null){
                    mOnItemClickListener.onWlmcClick(position,bean.getPartname(),viewHolder.mTextview3);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTextview1, mTextview2, mTextview3, mTextview4, mTextview5;
        EditText mEtCks;

        public ViewHolder(View itemView) {
            super(itemView);
            mTextview1 = itemView.findViewById(R.id.textview1);
            mTextview2 = itemView.findViewById(R.id.textview2);
            mTextview3 = itemView.findViewById(R.id.textview3);
            mTextview4 = itemView.findViewById(R.id.textview4);
            mTextview5 = itemView.findViewById(R.id.textview5);
            mEtCks = itemView.findViewById(R.id.et_cks);


        }

    }

    //两次点击按钮之间的间隔，目前为1000ms
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private long lastClickTime;

    public static interface OnItemClickListener {

        void onItemClick(View view, int position);
        void onWlbmClick(int position, String context, TextView a);
        void onWlmcClick(int position, String context, TextView a);

    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            long curClickTime = System.currentTimeMillis();
            if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
                lastClickTime = curClickTime;
                //注意这里使用getTag方法获取position
                mOnItemClickListener.onItemClick(v, (int) v.getTag());
            }
        }
    }

    public void setOnItemClickListener(MMMDetailsAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
